//
//  YDLoginController.m
//  YDRACLearn
//
//  Created by rowena on 2019/1/9.
//  Copyright © 2019年 wuyezhiguhun. All rights reserved.
//

#import "YDLoginController.h"
#import "YDLoginView.h"
#import "YDLoginViewModel.h"

@interface YDLoginController ()
/** 创建登录View */
@property (nonatomic, strong) YDLoginView *loginView;
/** 创建登录的ViewModel */
@property (nonatomic, strong) YDLoginViewModel *loginViewModel;
@end

@implementation YDLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addAllViews];
    [self setAllViewsLayout];
    [self associationViewOrViewModel];
}


#pragma mark ==== 添加所有的View ====
/**
 * 添加所有的View
 */
- (void)addAllViews {
    [super addAllViews];
    [self.view addSubview:self.loginView];
}
#pragma mark ==== 设置所有的View UI布局 ====
/**
 * 设置所有的View UI布局
 */
- (void)setAllViewsLayout {
    [super setAllViewsLayout];
    [self.loginView autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.loginView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.loginView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.loginView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
}

#pragma mark ==== 关联 ViewModel 和 View
- (void)associationViewOrViewModel {
    RAC(self.loginViewModel, userName) = self.loginView.userNameField.rac_textSignal;
    RAC(self.loginViewModel, password) = self.loginView.passwordField.rac_textSignal;
    RAC(self.loginView.loginButton, enabled) = self.loginViewModel.loginButtonSignal;
    
    //点击了登录按钮
    [[self.loginView.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        NSLog(@"LoginController -->: 点击了登录按钮");
        [self.loginViewModel.loginCommand execute:nil];
        
    }];
    
}

#pragma mark ==== get 函数 ====
- (YDLoginViewModel *)loginViewModel {
    if (!_loginViewModel) {
        _loginViewModel = [[YDLoginViewModel alloc] init];
    }
    return _loginViewModel;
}
- (YDLoginView *)loginView {
    if (!_loginView) {
        _loginView = [[YDLoginView alloc] init];
    }
    return _loginView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
